<template>
	<zh-form ref="paramsForm" :model="state.form" style="margin-top: 20px">
		<zh-form-input prop="paramKey" label="参数标识" placeholder="请输入参数标识" v-model="state.form.paramKey"></zh-form-input>
		<zh-form-input prop="paramName" label="参数名称" placeholder="请输入参数名称" v-model="state.form.paramName"></zh-form-input>
		<zh-form-select prop="status" label="状态" placeholder="请选择" :options="state.statusDict" v-model="state.form.status"></zh-form-select>
		<zh-form-select prop="fixed" label="是否可编辑" placeholder="请选择" :options="state.dictData.flag" v-model="state.form.fixed"></zh-form-select>
		<zh-form-input prop="classify" label="所属分类" placeholder="" v-model="state.form.classify"></zh-form-input>
		<zh-form-select
			placeholder="请搜索，单选"
			prop="orgId"
			funcCode="org0001"
			label="所属组织"
			searchField="orgName"
			:fieldNames="{
				label: 'orgName',
				value: 'orgId',
			}"
			:lazy="true"
			v-model="state.form.orgId"
		>
		</zh-form-select>
		<zh-form-cascader
			prop="areaCode"
			label="行政区划"
			placeholder="请选择"
			v-model="state.form.areaCode"
			:props="{ checkStrictly: true, emitPath: false }"
			:options="state.areaCodeSelectTreeData"
		></zh-form-cascader>
		<zh-form-input prop="orderNo" label="排序" placeholder="" v-model="state.form.orderNo"></zh-form-input>
		<zh-form-input prop="paramValue"  type="textarea" :span="24"  label="参数值" placeholder="请输入参数值" v-model="state.form.paramValue"></zh-form-input>
		
		<zh-form-input prop="remarks" type="textarea" :span="24" label="备注" v-model="state.form.remarks"></zh-form-input>
	</zh-form>
</template>

<script lang="ts" setup name="SystemParamsForm">
//----------------定义接收参数-------------------------------
const props = defineProps({
	paramsInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const paramsForm = ref<any>({});

//------------------内部变量定义-------------------------
const state = reactive({
	statusDict: [
		{
			dictKey: '0',
			dictValue: '禁用',
		},
		{
			dictKey: '1',
			dictValue: '启用',
		},
	],
	areaCodeSelectTreeData: [],
	dictData: {} as any,
	orgSelectTreeData: [],
	form: {} as any,
	initData: {} as any,
});

//------------------定义调用方法-------------------------
function getAreaCodeData() {
	setTimeout(() => {
		$api.common.send.post('dict0026', {}).then((res) => {
			state.areaCodeSelectTreeData = res.result;
		});
	}, 20);
}
function saveParams() {
	// 表单验证
	return new Promise((resolve, inject) => {
		paramsForm.value.validate((valid) => {
			if (valid) {
				const systemParams: any = state.form;
				if (systemParams.sysId) {
					setTimeout(() => {
						$api.common.send
							.post('sysCommon0006', systemParams)
							.then(() => {
								resolve(true);
								emit('update:modelValue', false);
								$messageBox.success('修改成功', '成功提示');
							})
							.catch(() => {
								inject();
							});
					}, 20);
				} else {
					setTimeout(() => {
						$api.common.send
							.post('sysCommon0005', systemParams)
							.then(() => {
								$messageBox.success('新增成功', '成功提示');
								resolve(false);
								emit('update:modelValue', false);
							})
							.catch(() => {
								inject();
							});
					}, 20);
				}
			}
		});
	});
}

//------------------初始化------------------------------
if (props.paramsInfo) {
	state.initData = Object.assign({}, props.paramsInfo);
	state.form = state.initData;
} else {
	state.initData = {};
}
const dictParams = {
	dictFields: 'flag',
};
$api.common.getBatchDict.post(dictParams).then((res) => {
	state.dictData = res.result;
});
getAreaCodeData()
defineExpose({ saveParams });
</script>
